import os

import jinja2


class CommonUtil(object):
    def get_email_subject_content(message=None):
        """Get the email subject content for exceptions."""
    # For a ti from DB (without ti.task), return the default value
    # Reuse it for smart sensor to send default email alert

        default_subject = 'Airflow alert: {{ti}}'
        # For reporting purposes, we report based on 1-indexed,
        # not 0-indexed lists (i.e. Try 1 instead of
        # Try 0 for the first attempt).
        default_html_content = (
            message
        )

        default_html_content_err = (
            message
        )

        jinja_context = {'ti': message}
        # This function is called after changing the state
        # from State.RUNNING so need to subtract 1 from self.try_number.

        jinja_env = jinja2.Environment(
            loader=jinja2.FileSystemLoader(os.path.dirname(__file__)), autoescape=True
        )
        subject = jinja_env.from_string(default_subject).render(**jinja_context)
        html_content = jinja_env.from_string(default_html_content).render(**jinja_context)
        html_content_err = jinja_env.from_string(default_html_content_err).render(**jinja_context)

        return subject, html_content, html_content_err